package com.ai.trickycards.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.ai.trickycards.AppViewModel
import com.ai.trickycards.R
import com.ai.trickycards.databinding.DialogCongratsBinding
import com.ai.trickycards.entity.enumeration.MedalEnum
import com.ai.trickycards.widgets.PokerCardView

@SuppressLint("SetTextI18n")
class CongratsDialog(
    context: Context,
    private val appViewModel: AppViewModel,
    private val medalEnum: MedalEnum,
    private var claimBlock: () -> Unit
) : Dialog(context) {

    private val binding by lazy {
        DialogCongratsBinding.inflate(layoutInflater)
    }

    val userInfo = appViewModel.userInfoFlow.value.copy()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        //setCanceledOnTouchOutside(false)
        //setCancelable(false)
        window?.let {
            it.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            val displayMetrics = context.resources.displayMetrics
            it.attributes.width = (displayMetrics.widthPixels - 48 * displayMetrics.density).toInt()
        }

        binding.tvLevel.text = "level ${medalEnum.level}"

        // 用户未Claim的背景、CardFace和CardStyle中每个取一个元素出来放入集合，随后从集合中随机取一个元素出来Claim给用户
        val complexList = mutableListOf<Any>()

        // 未Claim的背景
        listOf(
            R.mipmap.background01,
            R.mipmap.background02,
            R.mipmap.background03,
            R.mipmap.background04,
            R.mipmap.background05
        ).filter {
            !userInfo.ownGameBgList.contains(it)
        }.let {
            if (it.isNotEmpty()) {
                complexList.add(it[0])
            }
        }

        // 未Claim的CardFace
        PokerCardView.ShowType.entries.take(5).filter {
            !userInfo.ownCardFaceList.contains(it)
        }.let {
            if (it.isNotEmpty()) {
                complexList.add(it[0])
            }
        }

        // 未Claim的CardBackStyle
        PokerCardView.ShowType.entries.drop(5).filter {
            !userInfo.ownBackStyleList.contains(it)
        }.let {
            if (it.isNotEmpty()) {
                complexList.add(it[0])
            }
        }

        val one = complexList.shuffled()[0]
        if (one is Int) {
            doClaimBackground(one)
        } else if (one is PokerCardView.ShowType) {
            if (one.ordinal < PokerCardView.ShowType.Back01.ordinal) {
                doClaimCardFace(one)
            } else {
                doClaimCardBackStyle(one)
            }
        }

        binding.tvContent.setOnClickListener {
            appViewModel.changeUserInfo(userInfo.copy())
        }
    }

    private fun doClaimBackground(bg: Int) {
        binding.ivContent.isVisible = true
        binding.pokerView.isVisible = false
        binding.ivContent.setImageResource(bg)
        binding.tvContent.text = "Background"

        binding.tvClaim.setOnClickListener {
            it.isEnabled = false
            dismiss()
            userInfo.ownGameBgList.add(bg)
            userInfo.claimMedalList.add(medalEnum)
            appViewModel.changeUserInfo(userInfo)
            claimBlock.invoke()
        }
    }

    private fun doClaimCardFace(type: PokerCardView.ShowType) {
        binding.ivContent.isInvisible = true
        binding.pokerView.isVisible = true
        binding.pokerView.showType = type
        binding.tvContent.text = "Card face"

        binding.tvClaim.setOnClickListener {
            it.isEnabled = false
            dismiss()
            userInfo.ownCardFaceList.add(type)
            userInfo.claimMedalList.add(medalEnum)
            appViewModel.changeUserInfo(userInfo)
            claimBlock.invoke()
        }
    }

    private fun doClaimCardBackStyle(type: PokerCardView.ShowType) {
        binding.ivContent.isInvisible = true
        binding.pokerView.isVisible = true
        binding.pokerView.showType = type
        binding.tvContent.text = "Back style"

        binding.tvClaim.setOnClickListener {
            it.isEnabled = false
            dismiss()
            userInfo.ownBackStyleList.add(type)
            userInfo.claimMedalList.add(medalEnum)
            appViewModel.changeUserInfo(userInfo)
            claimBlock.invoke()
        }
    }
}